package cn.wolfcode.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 数据管理之字典明细
 */
@Api(value = "字典明细",description = "字典明细资源控制器")
@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiresPermissions(value = {"systemDictionaryItem:list","字典明细列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "systemDictionaryItem/list";
    }
    // 处理删除请求
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list.do"; // 让浏览器重新发一次请求
    }
    //新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        JsonResult jr = new JsonResult();
        System.out.println(systemDictionaryItem.getJson());
        try {
            //判断是否有序列sequence，如果没有，给一个默认的，在数据库中最大的序列，并给它加一
            if (systemDictionaryItem.getSequence() == null){
                int maxCount = systemDictionaryItemService.selectMaxSequenceByParentId(systemDictionaryItem.getParentId());
                systemDictionaryItem.setSequence(maxCount + 1);
            }
            if(systemDictionaryItem.getId() == null){ // 新增
                systemDictionaryItemService.save(systemDictionaryItem);
            }else {
                systemDictionaryItemService.update(systemDictionaryItem); // 修改
            }
            jr.setFlag(true);
            jr.setMsg("保存成功");
        } catch (Exception e) {
            jr.setFlag(false);
            jr.setMsg("保存失败");
        }
        return jr;
    }
}

